package com.oocl.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import com.oocl.service.CommandService;
import com.oocl.service.impl.CommandServiceImpl;

/**
 * Server port:8888
 * @author GANAB
 *
 */
public class Server {

	
	public static void main(String[] args) {
		CommandService service = new CommandServiceImpl();
		ServerSocket server = null;
		BufferedWriter writer = null;
		BufferedReader reader = null;
		
		try {
			server = new ServerSocket(8888);
			Socket socket = server.accept();

			while(true){
				writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				writer.write("Welcome! please use command:  ");
				writer.flush();
				
				reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String params = reader.readLine();
				StringBuilder sb = service.service(params);
				if(sb!=null){
					writer.write(sb.toString());	
				}else{
					writer.write("No result return.\r\n");
				}
				writer.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
